/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  UUcode module.
 */

(function(){

'use strict';

var UUcode = {

	generalDeRegExp : /#(UTB|bilibili|SlideShare|SoundCloud)#/i,
	supportMsg : '目前支持 Youtube; Bilibili; SlideShare; SoundCloud;',
	codeInsert : function(){
		var editarea = $(".tb-editor-editarea");
		if(editarea.$text().match(UUcode.generalDeRegExp))
			return;
		var input = prompt(UUcode.supportMsg);
		if(!input)
			return;

		var key = '';
		for(var patternName in UUcode.Pattern) {
			if(UUcode.Pattern[patternName].enRegExp.test(input)) {
				key = RegExp.$1;
				editarea.innerHTML += '#' + patternName + '#' + key + ' ';
				break;
			}
		}
	},
	Decode : function (content, patternName) {
		patternName = patternName.toLowerCase();
		var childNodes = content.childNodes;
		var pattern = this.Pattern[patternName];
		var id, found = false;
		for(var i = 0, l = childNodes.length; i < l; i++) {
			if(pattern.deRegExp.test(childNodes[i].textContent)) {
				id = RegExp.$1;
				found = true;
				break;
			}
		}
		if(!found)
			return;
		var wrapper = document.createElement('section');
		var title = document.createElement('p');
		title.style.color = "Green";
		title.innerText = pattern.title;
		var mainElem = document.createElement(pattern.mainElementTagName).attr(pattern.generateAttributes(id));
		wrapper.pushChild(title).pushChild(mainElem);
		content.replaceChild(wrapper, childNodes[i]);
	},
	Pattern : {
		__proto__ : null,
		utb : {
			title : '/# Youtube 视频 #/',
			enRegExp : /youtu\.?be.*?\/([A-Za-z0-9_-]{11})/,
			deRegExp : /#UTB#\W*([A-Za-z0-9_-]{11})/i,
			mainElementTagName : 'iframe',
			generateAttributes : function(id) {
				return {
					'class' : 'youtube',
					width : '95%',
					height : '450',
					src : "https://www.youtube.com/embed/" + id +'?theme=dark&color=red'
				}
			}
		},
		bilibili : {
			title : '/# Bilibili 视频 #/',
			enRegExp : /(?:bilibili\.tv.+av|loli\.my.*aid=)(\d+)/,
			deRegExp : /#bilibili#\W*(?:av)??(\d+)/i,
			mainElementTagName : 'embed',
			generateAttributes : function(id) {
				return {
					'class' : 'bilibili',
					width : '544',
					height : '452',
					type : "application/x-shockwave-flash",
					src : "http://static.loli.my/miniloader.swf",
					pluginspage : "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash",
					flashvars : 'aid=' + id
				}
			}
		},
		slideshare : {
			title : '/# SlideShare #/',
			enRegExp : /slideshare\.net\/slideshow\/embed_code\/(\d+)/,
			deRegExp : /#SlideShare#\W*(\d+)/i,
			mainElementTagName : 'iframe',
			generateAttributes : function(id) {
				return {
					width : '595',
					height : '497',
					src : 'http://www.slideshare.net/slideshow/embed_code/' + id
				}
			}
		},
		soundcloud : {
			title : '/# SoundCloud #/',
			enRegExp : /soundcloud.*((?:tracks|playlists)%2F(?:\d+))/,
			deRegExp : /#SoundCloud#\W*((?:tracks|playlists)%2F(?:\d+))/i,
			mainElementTagName : 'iframe',
			generateAttributes : function(id){
				return {
					width : '100%',
					height : '166',
					src : 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2F' +
							id + '&amp;auto_play=false&amp;show_artwork=true&amp;color=ff7700'
				}
			}
		}

	}

};


window.UUcode = UUcode;


})();